// pages/childonlinedetail/childonlinedetail.js
const util = require('../../utils/util.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    timer: null,
    endTime: null,
    timelength: null,
    url:null,
    preTime: new Date(),
    count:0,
    iosflag:false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    try {
      var res = wx.getSystemInfoSync()
      if(res.platform=="ios"){
         this.setData({
          iosflag:true
         })
      }
      console.log(res.platform)    
      } catch (e) {
      
      
      }

    wx.hideShareMenu();
    var timelength = options.timelength
    this.setData({
      endtime: options.endtime,
      timelength: timelength,
      preTime: new Date(),
      url:app.globalData.zhiboUrl
    })

     this.startReportHeart()
  },

  // finished() {
  //   Toast('倒计时结束');
  // },
  startReportHeart() {
    var that = this
    var timerTem = setTimeout(function () {
      var currentTime = util.formatTime1(new Date())
      if (currentTime >= that.data.endtime && that.data.count==0) {
        wx.showToast({
          title: "直播时间已到",
          icon: 'none',
          duration: 10000,
          mask: true
        })
        that.setData({
          count:1
        })
        that.startReportHeart()
        //  clearTimeout(that.data.timer)
        // wx.navigateBack({
        //   url: '../childonline/childonline'
        // })
      } else {

        var currTime = new Date()
        var cha = currTime.getTime() - that.data.preTime.getTime();
        // if (cha / (1000 * 60) >= that.data.timelength)
        if (cha / (1000 * 60) >= that.data.timelength && that.data.count==0) {
          wx.showToast({
            title: "您已经观看直播" + that.data.timelength + "分钟,直播会在10秒之后关闭，请注意用眼",
            icon: 'none',
            duration: 10000,
            mask: true
          })
          //  clearTimeout(that.data.timer)
          that.setData({
            count:1
          })
          that.startReportHeart()
        } else {
          if(that.data.count<10){
            if(that.data.count>0){
              var a=that.data.count
              a=a+1
              that.setData({
                count:a
              })
            }
            that.startReportHeart()
          }
          else{
            wx.navigateBack({
              url: '../childonline/childonline'
            })
          }
          
          
        }

      }


    }, 1000)
    // 保存定时器name
    that.setData({
      timer: timerTem
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearTimeout(this.data.timer)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})